﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Experiment.BLL;


namespace Experiment
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int counter = (int)Session["CurrentDoc"];
                if (counter == 0)
                    Previous.Enabled = false;
                List<Document> allDoc = new List<Document>((List<Document>)Session["DocumentList"]);
                if (counter == allDoc.Count - 1)
                    Next.Enabled = false;

                DocName.Text =allDoc[counter].Name;
                DocText.Text = allDoc[counter].Text.ToString();

                Dictionary<string, int[]> ans = (Dictionary<string, int[]>)Session["answers"];
                if (ans.ContainsKey(allDoc[counter].Name))
                {
                    if (ans[allDoc[counter].Name][0] != 0)
                        Question1.Items.FindByValue(" " + (ans[allDoc[counter].Name][0]).ToString()).Selected = true;
                    if (ans[allDoc[counter].Name][1] != 0)
                        Question2.Items.FindByValue(" " + (ans[allDoc[counter].Name][1]).ToString()).Selected = true;
                    if (ans[allDoc[counter].Name][2] != 0)
                        Question3.Items.FindByValue(" " + (ans[allDoc[counter].Name][2]).ToString()).Selected = true;

                }


            }
        }


        protected void Previous_Click(object sender, EventArgs e)
        {
            Session["CurrentDoc"] = (int)Session["CurrentDoc"] - 1;
            int counter = (int)Session["CurrentDoc"];
            Response.Redirect("DisplayText.aspx");
        }

        protected void Next_Click(object sender, EventArgs e)
        {
            Session["CurrentDoc"] = (int)Session["CurrentDoc"] + 1;
            int counter = (int)Session["CurrentDoc"];
            Response.Redirect("DisplayText.aspx");
        }

        public void Question1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int[] ansArray;
            int counter = (int)Session["CurrentDoc"];

            List<Document> allDoc = new List<Document>((List<Document>)Session["DocumentList"]);
            Dictionary<string, int[]> ans = (Dictionary<string, int[]>)Session["answers"];
            if (ans.ContainsKey(allDoc[counter].Name))
            {
                ans[allDoc[counter].Name][0] = Question1.SelectedIndex + 1;
                Session["answers"] = ans;
            }

            else
            {
                ansArray = new int[3];
                ansArray[0] = Question1.SelectedIndex + 1;
                ans.Add(allDoc[counter].Name, ansArray);
                Session["answers"] = ans;

            }

        }

        protected void Question2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int[] ansArray;
            int counter = (int)Session["CurrentDoc"];

            List<Document> allDoc = new List<Document>((List<Document>)Session["DocumentList"]);
            Dictionary<string, int[]> ans = (Dictionary<string, int[]>)Session["answers"];
            if (ans.ContainsKey(allDoc[counter].Name))
            {
                ans[allDoc[counter].Name][1] = Question2.SelectedIndex + 1;
                Session["answers"] = ans;
            }

            else
            {
                ansArray = new int[3];
                ansArray[1] = Question2.SelectedIndex + 1;
                ans.Add(allDoc[counter].Name, ansArray);
                Session["answers"] = ans;

            }


        }

        protected void Question3_SelectedIndexChanged(object sender, EventArgs e)
        {
            int[] ansArray;
            int counter = (int)Session["CurrentDoc"];

            List<Document> allDoc = new List<Document>((List<Document>)Session["DocumentList"]);
            Dictionary<string, int[]> ans = (Dictionary<string, int[]>)Session["answers"];
            if (ans.ContainsKey(allDoc[counter].Name))
            {
                ans[allDoc[counter].Name][2] = Question3.SelectedIndex + 1;
                Session["answers"] = ans;
            }

            else
            {
                ansArray = new int[3];
                ansArray[2] = Question3.SelectedIndex + 1;
                ans.Add(allDoc[counter].Name, ansArray);
                Session["answers"] = ans;

            }


        }
    }
}
